import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:ui';
import 'package:huxiaodui/api/model/alarm_model.dart';
import 'package:huxiaodui/api/model/notice_model.dart';
import 'package:huxiaodui/background_run/bg_channel.dart';
import 'package:huxiaodui/background_run/bg_task.dart';
import 'package:huxiaodui/background_run/mqtt.dart';
import 'package:huxiaodui/background_run/mqtt_send.dart';
import 'package:huxiaodui/config.dart';
import 'package:huxiaodui/utils/position.dart';
import 'package:huxiaodui/store/store.dart';
import 'package:huxiaodui/utils/tools/notification_tool.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_background_service_android/flutter_background_service_android.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_ringtone_player/flutter_ringtone_player.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../store/set.dart';

Future<void> backgroundServiceInit() async {

  final service = FlutterBackgroundService();
  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'foreground', // id
    '后台运行', // title
    importance: Importance.low,
  );

  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  if (Platform.isIOS || Platform.isAndroid) {
    await flutterLocalNotificationsPlugin.initialize(
      const InitializationSettings(
        iOS: DarwinInitializationSettings(),
        android: AndroidInitializationSettings('ic_bg_service_small'),
      ),
    );
  }

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);
  var packageInfo = await PackageInfo.fromPlatform();

  await service.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: false,
      notificationChannelId: 'foreground',
      initialNotificationTitle: packageInfo.appName,
      initialNotificationContent: '运行中...',
      foregroundServiceNotificationId: 1,
    ),
    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );
}

@pragma('vm:entry-point')
Future<bool> onIosBackground(ServiceInstance service) async {
  WidgetsFlutterBinding.ensureInitialized();
  DartPluginRegistrant.ensureInitialized();
  return true;
}

/* 后台运行任务 */
@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
  DartPluginRegistrant.ensureInitialized();
  var packageInfo = await PackageInfo.fromPlatform();
  if (service is AndroidServiceInstance) {
    service.setAsForegroundService();
    service.setForegroundNotificationInfo(title: packageInfo.appName, content: '运行中...');
  }
  await Store.init();
  await MQTTTask.init(service);
  await PositionTask.init(service);
}
